<?php

namespace app\admin\validate\user;

use think\Validate;
error_reporting(E_ERROR);
class Config extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'dpi' => 'checkDpi:thinkphp',
        'probe' => 'checkProbe:thinkphp',
        'user_id' => 'require|unique:user_config',
    ];
    /**
     * 提示消息
     */
    protected $message = [
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];
    public function __construct(array $rules = [], $message = [], $field = [])
    {
        $this->field = [
            'user_id' => __('User_id')
        ];
        parent::__construct($rules, $message, $field);
    }
    // 自定义验证规则
    /*****************************************
    {
        "url":"https://10.0.140.27",
        "username":"admin",
        "password":"panabit",
        "apps":[
            {
                "appid":"baidu",
                "appname":"百度搜索"
            },
            {
                "appid":"http",
                "appname":"WWW"
            }
        ],
        "tag":""
    }
    *******************************************/
    // protected function checkDpi($value,$rule,$data=[])
    // {   
    //     $is_json = is_json($value);
    //     $dpi = __('Dpi');
    //     if(is_json($value)){
    //         $array_value = json_decode($value,true);
    //         if(!isset($array_value['url'])){
    //             return "{$dpi}：json中url属性不存在";
    //         }
    //         if(empty($array_value['url'])){
    //             return "{$dpi}：json中url属性值不能为空";
    //         }
    //         if(!isset($array_value['username'])){
    //             return "{$dpi}：json中username属性不存在";
    //         }
    //         if(empty($array_value['username'])){
    //             return "{$dpi}：json中username属性值不能为空";
    //         }
    //         if(!isset($array_value['password'])){
    //             return "{$dpi}：json中password属性不存在";
    //         }
    //         if(empty($array_value['password'])){
    //             return "{$dpi}：json中password属性值不能为空";
    //         }
    //         $apps = $array_value['apps'];
    //         if(!isset($array_value['apps'])){
    //             return "{$dpi}：json中apps属性不存在";
    //         }
    //         if(!is_array($array_value['apps'])){
    //             return '{$dpi}：json中apps属性值不合法，形如[{"appid":"xx","appname":"xx"},{"appid":"xx","appname":"xx"}]';  
    //         }
    //         if(empty($array_value['apps'])){
    //             return "{$dpi}：json中apps属性值不能为空";
    //         }
    //         foreach($apps as $key => $value){
    //             if(!(is_array($value) && count($value)>0)){
    //                 return '{$dpi}：json中apps属性值格式不正确，形如[{"appid":"xx","appname":"xx"},{"appid":"xx","appname":"xx"}]';
    //             }
    //             if(!isset($value['appid'])){
    //                return "{$dpi}：json中apps属性值中appid属性不存在"; 
    //             }
    //             if(empty($value['appid'])){
    //                 return "{$dpi}：json中apps属性值中appid不能为空";
    //             }
    //             if(!isset($value['appname'])){
    //                return "{$dpi}：json中apps属性值中appname属性不存在"; 
    //             }
    //             if(empty($value['appname'])){
    //                 return "{$dpi}：json中apps属性值中appname不能为空";
    //             }
    //         }
    //         // die;
    //         return true;
    //     }else{
    //         return "{$dpi}：请输入合法的json数据";
    //     }
    // }
    /**
        {
            "domain":"http://122.115.224.149:8007",
            "tags":[
                {
                    "tag":"声学所",
                    "data":{
                        "interface":"/api/es/data_reports",
                        "param":{
                            "IpIfIndex":"103.230.96.225:52"
                        }
                    }
                },
                {
                    "tag":"高能所",
                    "data":{
                        "interface":"/api/big/trendday",
                        "param":{
                            "region":"高能所",
                            "namearr":"服务器吐流,缓存流量"
                        }
                    }
                }
            ]
        }
    **/
    protected function checkDpi($value,$rule,$data=[])
    {   
        $is_json = is_json($value);
        $dpi = __('Dpi');
        if(is_json($value)){
            $array_value = json_decode($value,true);
            if(!isset($array_value['domain'])){
                return "{$dpi}：json中url属性不存在";
            }
            if(empty($array_value['domain'])){
                return "{$dpi}：json中url属性值不能为空";
            }
            $apps = $array_value['tags'];
            if(!isset($array_value['tags'])){
                return "{$dpi}：json中tags属性不存在";
            }
            if(!is_array($array_value['tags'])){
                return '{$dpi}：json中tags属性值不合法，形如[{"appid":"xx","appname":"xx"},{"appid":"xx","appname":"xx"}]';  
            }
            if(empty($array_value['tags'])){
                return "{$dpi}：json中tags属性值不能为空";
            }
            // dump($apps);die;
            foreach($apps as $key => $value){
                if(!(is_array($value) && count($value)>0)){
                    return '{$dpi}：json中apps属性值格式不正确，形如[{"tag":"xxx","data":{"interface":"urlxxx","param":{"IpIfIndex":"xxxx"}}}]';
                }
                if(!isset($value['tag'])){
                   return "{$dpi}：json中tags属性值中tag属性不存在"; 
                }
                if(empty($value['tag'])){
                    return "{$dpi}：json中tags属性值中tag不能为空";
                }

                if(!isset($value['data'])){
                   return "{$dpi}：json中apps属性值中appname属性不存在"; 
                }
                if(empty($value['data'])){
                    return "{$dpi}：json中apps属性值中appname不能为空";
                }

                if(!isset($value['data']['interface'])){
                   return "{$dpi}：json中tags属性值中data属性值中interface属性不存在"; 
                }
                if(empty($value['data']['interface'])){
                    return "{$dpi}：json中tags属性值中data属性值中interface属性不能为空";
                }
                // die;
                // if(!(is_array($value) && count($value)>0)){
                //     return '{$dpi}：json中apps属性值格式不正确，形如[{"tag":"xxx","data":{"interface":"urlxxx","param":{"IpIfIndex":"xxxx"}}}]';
                // }
                // json中apps属性值中webpage属性值中linkid不能为空
                // $value['data']['interface']
                // dump($value['data']['param']);die;
                // dump();die;
            }
            // die;
            return true;
        }else{
            return "{$dpi}：请输入合法的json数据";
        }
    }
    /********************************************
    {
        "url":"http://10.0.120.192:9005/quality/linkQualityViewDetailController/api",
        "missionid":{
            "webpage":[
                "C83EA07CBB200001DC94310017609FD0"
            ],
            "filedownload":[
                "C83EA07CBB200001DC94310017609FD0"
            ],
            "basedlink":[
                "C83EA07CBB200001DC94310017609FD0"
            ]
        }
    }
    {
        "url": "http://10.0.120.192:9005/quality/linkQualityViewDetailController/api",
        "mission": {
            "webpage": [
                {
                    "missionid": "C8495D2BD5F0000135BD647912671F14",
                    "linkid": [
                        4
                    ]
                }
            ],
            "filedownload": [
                {
                    "missionid": "C8495D0B59200001515388D71C2048D0",
                    "linkid": [
                        3
                    ]
                }
            ],
            "basedlink": [
                {
                    "missionid": "C8495C6A48C0000165B013DF1D7ECE30",
                    "linkid": [
                        1,
                        3,
                        4
                    ]
                },
                {
                    "missionid": "C8495D46C1C0000171C8140012431450",
                    "linkid": [
                        1,
                        3,
                        4
                    ]
                }
            ]
        }
    }
    *********************************************/
    protected function checkProbe($value,$rule,$data=[]){
        $is_json = is_json($value);
        if(is_json($value)){
            $array_value = json_decode($value,true);
            if(!isset($array_value['url'])){
               return "探针配置：json中url属性不存在";
            }
            if(empty($array_value['url'])){
                return "探针配置：json中url属性值不能为空";
            }
            if(!isset($array_value['mission'])){
               return "探针配置：json中mission属性不存在";
            }
            if(empty($array_value['mission'])){
                return "探针配置：json中mission属性值不能为空";
            }
            if(!is_array($array_value['mission'])){
                return '探针配置：json中mission属性值不合法,例如"mission": {"webpage": [{"missionid": "xx","linkid": [4]}],"filedownload": [{"missionid": "xx","linkid": [3]}],"basedlink": [{"missionid": "xx","linkid": [1, 3, 4]},{"missionid": "xx","linkid": [1, 3, 4]}]}';  
            }
            $missionid = $array_value['mission'];
            if(!isset($missionid['webpage'])){
                return "探针配置：json中mission属性值中webpage属性不存在";
            }
            if(!is_array($missionid['webpage'])){
               return "探针配置：json中mission属性值中webpage属性值不合法,例如\"webpage\": [{\"missionid\": \"xx\",\"linkid\": [4]}]";
            }
            if(empty($missionid['webpage'])){
               return "探针配置：json中mission属性值中webpage属性值不能为空";
            }
            foreach ($missionid['webpage'] as $k => $v) {
                if(!(is_array($v) && count($v)>0)){
                    return '探针配置：json中mission属性值中webpage属性值格式不合法:例如[{"missionid": "xx","linkid": [4]}]';
                }
                if(!isset($v['missionid'])){
                   return "探针配置：json中mission属性值中webpage属性值中missionid属性不存在"; 
                }
                if(empty($v['missionid'])){
                    return "探针配置：json中mission属性值中webpage属性值中missionid不能为空";
                }
                if(isset($v['linkid'])){
                    if(empty($v['linkid'])){
                       return "探针配置：json中mission属性值中webpage属性值中linkid不能为空"; 
                    }
                    if(!is_array($v['linkid'])){
                       return "探针配置：json中mission属性值中webpage属性值中linkid不合法,例如\"linkid\": [1,3,4]";
                    }
                    foreach ($v['linkid'] as $key1 => $value1) {
                       if(!preg_match("/^[1-9][0-9]*$/",$value1)){
                            return "探针配置：json中mission属性值中webpage属性值中linkid不是正整数,例如\"linkid\": [1,3,4]";
                        }
                    }
                }
                // if(is_array($v)){
                //     return "探针配置：json中missionid属性值中webpage属性值格式不合法";
                // }
                // if(empty($v)){
                //     return "探针配置：json中missionid属性值中webpage属性值不能为空";
                // }
            }
            if(!isset($missionid['filedownload'])){
                return "探针配置：json中mission属性值中filedownload属性不存在";
            }
            if(!is_array($missionid['filedownload'])){
               // return "探针配置：json中missionid属性值中filedownload属性值不合法";
               // return "探针配置：json中missionid属性值中filedownload属性值不合法,例如\"filedownload\":[\"xx\"]";
               return "探针配置：json中mission属性值中filedownload属性值不合法,例如\"filedownload\": [{\"missionid\": \"xx\",\"linkid\": [4]}]";
            }
            if(empty($missionid['filedownload'])){
               // return "探针配置：json中missionid属性值中filedownload属性值不能为空";
               return "探针配置：json中mission属性值中filedownload属性值不能为空";
            }
            foreach ($missionid['filedownload'] as $k => $v) {
                if(!(is_array($v) && count($v)>0)){
                    return '探针配置：json中mission属性值中filedownload属性值格式不合法:例如[{"missionid": "xx","linkid": [4]}]';
                }
                if(!isset($v['missionid'])){
                   return "探针配置：json中mission属性值中filedownload属性值中missionid属性不存在"; 
                }
                if(empty($v['missionid'])){
                    return "探针配置：json中mission属性值中filedownload属性值中missionid不能为空";
                }
                if(isset($v['linkid'])){
                    if(empty($v['linkid'])){
                       return "探针配置：json中mission属性值中filedownload属性值中linkid不能为空"; 
                    }
                    if(!is_array($v['linkid'])){
                       return "探针配置：json中mission属性值中filedownload属性值中linkid不合法,例如\"linkid\": [1,3,4]";
                    }
                    foreach ($v['linkid'] as $key1 => $value1) {
                       if(!preg_match("/^[1-9][0-9]*$/",$value1)){
                            return "探针配置：json中mission属性值中filedownload属性值中linkid不是正整数,例如\"linkid\": [1,3,4]";
                        }
                    }
                }
                // if(is_array($v)){
                //     return "探针配置：json中missionid属性值中filedownload属性值格式不合法";
                // }
                // if(empty($v)){
                //     return "探针配置：json中missionid属性值中filedownload属性值不能为空";
                // }
            }
            if(!isset($missionid['basedlink'])){
                return "探针配置：json中mission属性值中basedlink属性不存在";
            }
            if(!is_array($missionid['basedlink'])){
               // return "探针配置：json中missionid属性值中basedlink属性值不合法,例如\"basedlink\":[\"xx\"]";
               return "探针配置：json中mission属性值中basedlink属性值不合法,例如\"basedlink\": [{\"missionid\": \"xx\",\"linkid\": [4]}]";
            }
            if(empty($missionid['basedlink'])){
               return "探针配置：json中missionid属性值中basedlink属性值不能为空";
            }
            foreach ($missionid['basedlink'] as $k => $v) {
                if(!(is_array($v) && count($v)>0)){
                    return '探针配置：json中mission属性值中basedlink属性值格式不合法:例如[{"missionid": "xx","linkid": [4]}]';
                }
                if(!isset($v['missionid'])){
                   return "探针配置：json中mission属性值中basedlink属性值中missionid属性不存在"; 
                }
                if(empty($v['missionid'])){
                    return "探针配置：json中mission属性值中basedlink属性值中missionid不能为空";
                }
                if(isset($v['linkid'])){
                    if(empty($v['linkid'])){
                       return "探针配置：json中mission属性值中basedlink属性值中linkid不能为空"; 
                    }
                    if(!is_array($v['linkid'])){
                       return "探针配置：json中mission属性值中basedlink属性值中linkid不合法,例如\"linkid\": [1,3,4]";
                    }
                    foreach ($v['linkid'] as $key1 => $value1) {
                       if(!preg_match("/^[1-9][0-9]*$/",$value1)){
                            return "探针配置：json中mission属性值中basedlink属性值中linkid不是正整数,例如\"linkid\": [1,3,4]";
                        }
                    }
                }
                // if(is_array($v)){
                //     return "探针配置：json中missionid属性值中basedlink属性值格式不合法";
                // }
                // if(empty($v)){
                //     return "探针配置：json中missionid属性值中basedlink属性值不能为空";
                // }
            }
            return true;
        }else{
            return "探针配置：请输入合法的json数据";
        }
    }
}
